﻿using System;
using DotNetNeural.Data.Base;

namespace DotNetNeural.Projections
{
    /// <summary>
    /// Provides set of methods for scaling numbers and coordinates
    /// </summary>
    public class ScaleUtil
    {
        /// <summary>
        /// Minimum value for numbrers
        /// </summary>
        public float Min { get; private set; }
        
        /// <summary>
        /// Maximum value for numbers
        /// </summary>
        public float Max { get; private set; }

        public ScaleUtil(float min, float max)
        {
            if (min > max)
                throw new ArgumentException("Illegal min/max args. Min should be smaller then max");

            Min = min;
            Max = max;
        }

        public ScaleUtil(Interval<float> interval)
        {
            Min = interval.Left;
            Max = interval.Right;
        }

        public float Scale(Interval<float> limits, float x)
        {
            return Scale(limits.Left, limits.Right, x);
        }

        public float Scale(float left, float right, float x)
        {
            if (left > right)
                throw new ArgumentException("Illegal left/right args. Left should be smaller then max");

            if (x < left || x > right)
                throw new ArgumentException(
                    @"Illegal x value. Should be greater or equal then left 
                      and smaller or equal then right");

            float pratio = (x - left) / Math.Abs(right - left);
            return Math.Abs(Max - Min) * pratio;
        }
    }
}
